{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 项目介绍\n",
    "ERNIE Bot提供便捷易用的接口，可以调用文心大模型的能力，包含文本创作、通用对话、语义向量、AI作图等。\n",
    "\n",
    "使用步骤可以大致分为`安装-认证鉴权-模型调用`三个步骤。\n",
    "\n",
    "在模型调用方面目前主要提供有四类功能：对话补全(Chat Completion)，函数调用(Function Calling)，文本嵌入(Embedding)，文生图(Image Generation)。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 安装\n",
    "快速安装Python语言的最新版本ERNIE Bot（要求Python >= 3.8)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: erniebot in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (0.4.0)\n",
      "Requirement already satisfied: aiohttp in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from erniebot) (3.8.6)\n",
      "Requirement already satisfied: bce-python-sdk in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from erniebot) (0.8.92)\n",
      "Requirement already satisfied: colorlog in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from erniebot) (6.7.0)\n",
      "Requirement already satisfied: jsonschema>=4.19 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from erniebot) (4.19.2)\n",
      "Requirement already satisfied: requests>=2.20 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from erniebot) (2.31.0)\n",
      "Requirement already satisfied: typing-extensions in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from erniebot) (4.8.0)\n",
      "Requirement already satisfied: attrs>=22.2.0 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (23.1.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (2023.7.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (0.30.2)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (0.12.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from requests>=2.20->erniebot) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from requests>=2.20->erniebot) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from requests>=2.20->erniebot) (2.0.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from requests>=2.20->erniebot) (2023.7.22)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from aiohttp->erniebot) (6.0.4)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from aiohttp->erniebot) (4.0.3)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from aiohttp->erniebot) (1.9.2)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from aiohttp->erniebot) (1.4.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from aiohttp->erniebot) (1.3.1)\n",
      "Requirement already satisfied: pycryptodome>=3.8.0 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from bce-python-sdk->erniebot) (3.19.0)\n",
      "Requirement already satisfied: future>=0.6.0 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from bce-python-sdk->erniebot) (0.18.3)\n",
      "Requirement already satisfied: six>=1.4.0 in /opt/anaconda3/envs/ernie/lib/python3.10/site-packages (from bce-python-sdk->erniebot) (1.16.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install erniebot"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 认证鉴权\n",
    "\n",
    "使用ERNIE Bot之前，请首先申请并设置鉴权参数，详情参考[认证鉴权](../../docs/authentication.md)。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 参数配置\n",
    "ERNIE Bot参数配置，主要涉及认证鉴权、后端平台等信息，详情参考[参数配置](../../docs/configuration.md)。\n",
    "\n",
    "\n",
    "**注意事项**：\n",
    "* AI Studio每个账户的access token，有100万token的免费额度，可以用于ERNIE Bot调用文心大模型。\n",
    "* 在[token管理页面](https://aistudio.baidu.com/token/manage)可以查看token获取、消耗明细和过期记录，或者购买更多token。\n",
    "* access token是私密信息，切记不要对外公开。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 如果使用AI Studio（推荐使用），可以在个人中心的[访问令牌页面](https://aistudio.baidu.com/usercenter/token)获取用户凭证access token。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import erniebot\n",
    "\n",
    "erniebot.api_type = \"aistudio\"\n",
    "# 通过使用全局变量设置鉴权信息\n",
    "erniebot.access_token = \"<eb-access-token>\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 如果使用qianfan，在完成创建千帆应用后， 在[控制台](https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application)创建千帆应用，可以获取到API key与secret key。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import erniebot\n",
    "\n",
    "erniebot.api_type = \"qianfan\"\n",
    "erniebot.access_token = None  # Option\n",
    "\n",
    "# 通过使用全局变量设置鉴权信息\n",
    "erniebot.ak = \"<eb-ak>\"\n",
    "erniebot.sk = \"<eb-sk>\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 如果使用yinian（AI绘画功能），先需在智能创作页面中[开通AI绘画服务](https://console.bce.baidu.com/ai/#/ai/intelligentwriting/overview/index),激活AI绘画-高级功能后，进入在智能创作平台 - [应用页面](https://console.bce.baidu.com/ai/#/ai/intelligentwriting/app/list)，创建应用，可以拿到API key和secret key。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import erniebot\n",
    "\n",
    "erniebot.api_type = \"yinian\"\n",
    "erniebot.access_token = None  # Option\n",
    "\n",
    "# 直接使用全局变量设置鉴权信息\n",
    "erniebot.ak = \"<eb-ak>\"\n",
    "erniebot.sk = \"<eb-sk>\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 模型总览\n",
    "\n",
    "完成好上述步骤之后，就可以根据需求调用相关模型，ERNIE Bot支持的所有模型如下：\n",
    "\n",
    "| 模型名称 | 说明 | 功能 | 支持该模型的后端 | 输入token数量上限 |\n",
    "|:--- | :--- | :--- | :--- | :--- |\n",
    "| ernie-3.5 | 文心大模型3.5版本。具备优秀的知识增强和内容生成能力，在文本创作、问答、推理和代码生成等方面表现出色。 | 对话补全，函数调用 | qianfan，aistudio | 3000 |\n",
    "| ernie-turbo | 文心大模型。相比ernie-3.5模型具备更快的响应速度和学习能力，API调用成本更低。 | 对话补全 | qianfan，aistudio | 3000 |\n",
    "| ernie-4.0 | 文心大模型4.0版本，具备目前系列模型中最优的理解和生成能力。 | 对话补全，函数调用 | qianfan，aistudio | 3000 |\n",
    "| ernie-longtext | 文心大模型。在ernie-3.5模型的基础上增强了对长对话上下文的支持，输入token数量上限为7000。 | 对话补全，函数调用 | qianfan，aistudio | 7000 |\n",
    "| ernie-text-embedding | 文心百中语义模型。支持计算最多384个token的文本的向量表示。 | 语义向量 | qianfan，aistudio | 384*16 |\n",
    "| ernie-vilg-v2 | 文心一格模型。 | 文生图 | yinian | 200 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "API名称：ernie-bot,      模型名称：文心一言旗舰版\n",
      "API名称：ernie-bot-turbo,      模型名称：文心一言轻量版\n",
      "API名称：ernie-bot-4,      模型名称：基于文心大模型4.0版本的文心一言\n",
      "API名称：ernie-text-embedding,      模型名称：文心百中语义模型\n",
      "API名称：ernie-vilg-v2,      模型名称：文心一格模型\n"
     ]
    }
   ],
   "source": [
    "import erniebot\n",
    "\n",
    "# 您也可以通过命令查找模型\n",
    "models = erniebot.Model.list()\n",
    "for i in range(len(models)):\n",
    "    print(f\"API名称：{models[i][0]},      模型名称：{models[i][1]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 快速开始"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.1 对话补全(Chat Completion)\n",
    "文心大模型可以理解自然语言，并以文本输出与用户进行对话。将对话上下文与输入文本提供给模型，由模型给出新的回复，即为对话补全。对话补全功能可应用于广泛的实际场景，例如对话沟通、内容创作、分析控制、函数调用等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您好，我是文心一言，英文名是ERNIE Bot。我能够与人对话互动，回答问题，协助创作，高效便捷地帮助人们获取信息、知识和灵感。\n"
     ]
    }
   ],
   "source": [
    "import erniebot\n",
    "\n",
    "erniebot.api_type = \"aistudio\"\n",
    "erniebot.access_token = \"<eb-access-token>\"\n",
    "\n",
    "chat_message = [{\"role\": \"user\", \"content\": \"你好，请介绍一下你自己\"}]\n",
    "response = erniebot.ChatCompletion.create(model=\"ernie-4.0\", messages=chat_message)\n",
    "\n",
    "# 使用response.get_result()获得模型返回结果\n",
    "print(response.get_result())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.2 文本嵌入(Embedding)\n",
    "文本向量，是指将一段文本，转化为一定维度的向量（文心百中语义模型中为384维），其中相近语义、相关主题的文本在向量空间更接近。拥有一个良好的文本嵌入特征，对于文本可视化、检索、聚类、内容审核等下游任务，有着重要的意义，目前API接口可接受的batch_size单次最多支持16个，每段文本最多支持384token。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.12393086403608322, 0.06512520462274551, 0.05346716567873955, 0.054938241839408875, 0.01714814081788063, -0.08167827129364014, -0.023749373853206635, -0.05039228871464729, -0.040341075509786606, 0.05865912884473801, 0.016324903815984726, -0.058406684547662735, -0.04220706224441528, 0.0458282008767128, -0.1460632085800171, -0.049745965749025345, -0.03678134083747864, 0.012619715183973312, -0.014126688241958618, 0.0006569335819222033, 0.013071301393210888, -0.0018191564595326781, -0.04659661278128624, -0.05999888479709625, 0.02386806719005108, -0.033645354211330414, 0.08845698088407516, 0.07145956158638, -0.010486936196684837, -0.015010570175945759, -0.01926182582974434, -0.09276989102363586, -0.008814138360321522, -0.02573108859360218, -0.011305577121675014, 0.02599318139255047, 0.013190587051212788, 0.055894795805215836, -0.077104851603508, 0.010798984207212925, -0.05201827362179756, -0.01178425457328558, 0.04679083451628685, -0.006311427801847458, 0.07979213446378708, -0.05993827432394028, -0.10336479544639587, 0.060519710183143616, -0.008194743655622005, -0.02462303452193737, 0.008664045482873917, -0.019067654386162758, 0.06620414555072784, -0.036438774317502975, 0.030461542308330536, 0.012983747757971287, -0.027496762573719025, -0.02178688906133175, 0.0008967460598796606, -0.014411399140954018, -0.02170397713780403, -0.05739177390933037, 0.005925025325268507, -0.07930614799261093, 0.137408047914505, 0.017562543973326683, 0.04622232913970947, 0.027515241876244545, 0.027436144649982452, 0.018588175997138023, 0.004503807984292507, 0.021820982918143272, -0.08468001335859299, 0.08908464014530182, 0.07250522822141647, 0.020316563546657562, -0.08273280411958694, 0.04405013471841812, -0.022231735289096832, 0.014862153679132462, 0.038597412407398224, 0.03031317889690399, 0.061423856765031815, -0.012558488175272942, -0.055344682186841965, -0.0018919823924079537, -0.07665809988975525, -0.016824893653392792, 0.050464216619729996, -0.00357417156919837, -0.05618833750486374, -0.15275031328201294, 0.04941688850522041, -0.06676385551691055, -0.056054454296827316, 0.04359078034758568, -0.05236506089568138, -0.029834026470780373, 0.028620649129152298, -0.025159494951367378, -0.0587918683886528, -0.0703502744436264, 0.07646499574184418, -0.05493784695863724, 0.0710410475730896, -0.06597091257572174, -0.08634699881076813, -0.16756334900856018, 0.01845960132777691, -0.022447410970926285, -0.03926842659711838, 0.07917698472738266, -0.02364439144730568, 0.014074575155973434, 0.013737611472606659, 0.03448419272899628, -0.018709572032094002, -0.026274243369698524, 0.02445005625486374, -0.08247654885053635, -0.036668531596660614, -0.022490642964839935, -0.04927549511194229, 0.09152866899967194, -0.015470282174646854, -0.003777889534831047, -0.05837828665971756, 0.018777774646878242, 0.019315535202622414, 0.17089319229125977, 0.0035293952096253633, -0.002445742953568697, -0.009234469383955002, 0.02196548320353031, 0.10734690725803375, -0.002021083375439048, -0.0012763900449499488, -0.020174488425254822, -0.05045972391963005, 0.08091080188751221, -0.011431857012212276, 0.08671028912067413, 0.034442704170942307, -0.026053933426737785, 0.049069877713918686, 0.0013618639204651117, -0.013132759369909763, 0.07689011096954346, -0.04989981651306152, 0.054785747081041336, -0.043564192950725555, 0.02618328295648098, -0.014225582592189312, -0.022566767409443855, -0.06264572590589523, -0.034698326140642166, -0.0001107764765038155, -0.06152806431055069, 0.0036162040196359158, 0.01230692770332098, -0.05581643059849739, 0.010127565823495388, -0.05308711528778076, -0.05022891238331795, 0.0056801652535796165, -0.08951827138662338, -0.03046564571559429, 0.08251140266656876, 0.04728938266634941, -0.060433242470026016, 0.0033412182237952948, 0.012290587648749352, 0.07780375331640244, -0.02360345609486103, -0.07125856727361679, 0.049685221165418625, 0.07224086672067642, 0.11575620621442795, 0.008243431337177753, -0.012308630160987377, 0.053591471165418625, -0.07608630508184433, 0.029831329360604286, 0.013562287203967571, 0.024182721972465515, -0.017201408743858337, -0.03160925954580307, 0.03825448825955391, 0.008620260283350945, -0.03325319662690163, 0.01760943979024887, 0.06543662399053574, 0.04450875148177147, 0.010917714796960354, 0.009390872903168201, 0.03062949702143669, -0.0076032583601772785, -0.049751076847314835, -0.015538417734205723, -0.032042618840932846, 0.11950680613517761, -0.028337452560663223, 0.04041427746415138, 0.14753589034080505, 0.051742952316999435, 0.021051540970802307, 0.06310559809207916, -0.02798588015139103, 0.08760247379541397, 0.006532905623316765, 0.14526154100894928, -0.015541037544608116, -0.07818841189146042, -0.00386637425981462, -0.012766157276928425, 0.04967696964740753, -0.04228254780173302, -0.008131932467222214, 0.039440806955099106, 0.017025263980031013, 0.029931651428341866, -0.05010100454092026, 0.06069578975439072, -0.01839270070195198, -0.013055648654699326, 0.019720539450645447, 0.08475974947214127, 0.013340308330953121, 0.05732417106628418, -0.08631827682256699, 0.059385668486356735, -0.06374119222164154, -0.049451734870672226, 0.04297780618071556, -0.02166394330561161, 0.03173642233014107, -0.03146092966198921, -0.08326373249292374, 0.02655809000134468, -0.016991138458251953, -0.06750057637691498, -0.012286640703678131, 0.0010668501490727067, -0.014213801361620426, 0.03157174214720726, -0.052248887717723846, 0.05456520989537239, 0.11080439388751984, -0.06336615234613419, -0.03109496831893921, -0.0804644376039505, 0.006365587003529072, -0.016252659261226654, 0.039697032421827316, -0.03961373120546341, -0.02783684805035591, 0.07045438140630722, -0.05832531303167343, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.26078736782073975, -0.04141460731625557, 0.0, 0.0, -0.011947153136134148, 0.0, -0.02043531835079193, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.07810815423727036, 0.0, 0.0, -0.025322146713733673, -0.021555209532380104, -0.07156489044427872, 0.0, 0.0, 0.0, 0.0, -0.11648621410131454, 0.031780462712049484, 0.1278366893529892, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.116255983710289, 0.07236123085021973, 0.03442956134676933, 0.0, 0.0, -0.1583525538444519, 0.0, 0.0, 0.0, 0.0, -0.12039731442928314, 0.0, 0.0, 0.0, 0.02976030483841896, 0.0, -0.024193869903683662, 0.0, 0.0, 0.0, 0.0, 0.0, -0.0072584911249578, -0.07561428099870682, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.054427411407232285, 0.04511762410402298, 0.0, 0.0, 0.0, -0.15591853857040405, 0.10208409279584885, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.08332417905330658, 0.0, 0.0, 0.0, 0.0, -0.011994187720119953, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.042864102870225906, 0.0, 0.04319864511489868, 0.0, 0.09600488096475601, 0.0, 0.04153875634074211, 0.0, 0.0, 0.0, 0.05101964250206947, -0.11683668196201324, 0.0, -0.06753823906183243, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0383356437087059, 0.03771863132715225, 0.04175252839922905, 0.010017745196819305, -0.05751395970582962, -0.03792840614914894, -0.0937972366809845, -0.019642073661088943, 0.057318732142448425, -0.026970149949193, 0.026251478120684624, 0.0648733451962471, -0.12652482092380524, -0.015095271170139313, -0.08382508903741837, 0.07013081759214401, -0.002245674841105938, 0.008431381545960903, -0.027580611407756805, 0.07550489157438278, 0.041026435792446136, -0.016500204801559448, -0.014965462498366833, -0.03379667177796364, -0.03146945685148239, -0.06259060651063919, -0.013681524433195591, 0.014097284525632858, 0.050641197711229324, 0.05133596435189247, -0.07745523750782013, -0.06938300281763077, -0.14050441980361938, -0.0475454106926918, 0.026918312534689903, -0.015482773073017597, -0.06362009048461914, -0.05587482824921608, -0.0510423444211483, -0.05997798964381218, 0.031812556087970734, -0.03642956539988518, -0.06177765130996704, 0.046211618930101395, -0.04469788447022438, -0.0008917557424865663, 0.03602195158600807, 0.022225279361009598, 0.052277181297540665, -0.030573705211281776, -0.03179909288883209, -0.030528515577316284, -0.0004877384926658124, -0.0005554874078370631, 0.046489786356687546, -0.014454968273639679, -0.02400290220975876, -0.0032705236226320267, -0.04640588536858559, 0.02617749571800232, -0.03544134274125099, -0.05857739970088005, 0.0002576441038399935, -0.020024964585900307, 0.020577475428581238, -0.07063407450914383, 0.009608197025954723, 0.05706772580742836, 0.09540875256061554, -0.011207109317183495, -0.09445955604314804, -0.04102757200598717, 0.06686481088399887, -0.08190406113862991, -0.08889014273881912, 0.012513328343629837, 0.07017087936401367, 0.08179359138011932, 0.08599081635475159, -0.0023058783262968063, 0.043315403163433075, -0.061055682599544525, 0.14925910532474518, 0.06919527798891068, -0.000200674359803088, 0.06054820492863655, -0.01568685472011566, 0.025515977293252945, 0.09026706963777542, -0.011866739019751549, -0.09469518065452576, -0.010738806799054146, 0.1180713102221489, -0.021613607183098793, 0.0743296667933464, -0.06580383330583572, 0.03452248126268387, -0.05821526423096657, -0.013256930746138096, -0.1061810627579689, 0.021834244951605797, 0.04914559796452522, 0.08007513731718063, 0.022769322618842125, -0.0013164140982553363, -0.0274383332580328, -0.0472942590713501, -0.09362781047821045, 0.09019148349761963, -0.017591651529073715, -0.005589867942035198, 0.013755992986261845, -0.13341714441776276, 0.0011952678905799985, 0.005004548933357, -0.029787087813019753, -0.05613655969500542, 0.055325090885162354, 0.08982843160629272, 0.05322820693254471, -0.03743863105773926, -0.019141459837555885, -0.00701902573928237, -0.004055540543049574, 0.03443831205368042, 0.025469092652201653, 0.037712812423706055, 0.011830746196210384, -0.08496560156345367, 0.05180276930332184, 0.052730850875377655, 0.00244692200794816, 0.04022414982318878, -0.038521017879247665, -0.03969975560903549, 0.03449808806180954, 0.08938043564558029, -0.042106594890356064, -0.0017152040963992476, -0.00016503770893905312, -0.02158789336681366, 0.10519043356180191, -0.0019344021566212177, 0.08883883059024811, -0.09451425820589066, 0.03283997252583504, -0.05713285133242607, 0.05798697471618652, 0.0039007323794066906, 0.05501514673233032, -0.059636685997247696, -0.022920269519090652, -0.039060354232788086, -0.0444902703166008, -0.09592243283987045, 0.03234732151031494, -0.0853579118847847, -0.0555243082344532, -0.029872171580791473, 0.09014902263879776, -0.09516578912734985, 0.17798306047916412, -0.052431486546993256, 0.01680145598948002, 0.018374770879745483, -0.01934719830751419, 0.0334763340651989, 0.12820878624916077, 0.10495088994503021, -0.056222472339868546, 0.012443841435015202, 0.06117534264922142, 0.07372154295444489, -0.146345853805542, -0.06297747790813446, -0.11616487056016922, 0.025794843211770058, 0.06928903609514236, -0.055684853345155716, 0.05194629356265068, -0.09076684713363647, 0.043250489979982376, 0.002496603410691023, -0.04983661323785782, 0.08306154608726501, 0.0689687505364418, -0.10477741807699203, 0.08446181565523148, 0.028737124055624008, -0.10554316639900208, -0.07227646559476852, 0.06028304994106293, 0.13438726961612701, -0.018474513664841652, -0.0500834695994854, 0.011733565479516983, 0.03724290803074837, 0.049806348979473114, -0.029314903542399406, -0.07272937148809433, -0.04518107697367668, 0.07860397547483444, 0.01481136865913868, 0.12039242684841156, 0.0058028376661241055, -0.03334954380989075, -0.0637706071138382, 0.0331452377140522, 0.09146992862224579, 0.04051864147186279, -0.007694820873439312, 0.027361053973436356, -0.12709718942642212, -0.06480110436677933, 0.09247095882892609, -0.01159035973250866, -0.045780476182699203, -0.07050780951976776, 0.02705230563879013, -0.053999219089746475, 0.05256940424442291, 0.016404278576374054, 0.05830094590783119, 0.08317644149065018, 0.03479723259806633, -0.035504404455423355, 0.0337660126388073, 0.029436934739351273, 0.06948558986186981, -0.08364655077457428, -0.05376904085278511, -0.011519080027937889, -0.020604411140084267, 0.033282261341810226, -0.07212121784687042, 0.09828836470842361, 0.08516618609428406, -0.04038620367646217, 0.012143936939537525, -0.019138947129249573, -0.01972845569252968, -0.05065235495567322, 0.042912621051073074, -0.05205236002802849, 0.09151729941368103, -0.07050173729658127, 0.0910414382815361, 0.11697268486022949, -0.05766289681196213, -0.06095752492547035, -0.05423835664987564, -0.030191846191883087, -0.015662452206015587, -0.001722560147754848, -0.013289855793118477, 0.09511920064687729, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.017239468172192574, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.014331732876598835, 0.0, 0.0, 0.0, 0.0, 0.14296218752861023, 0.0, 0.0, -0.15629790723323822, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.09406696259975433, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.09040746092796326, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1273038387298584, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.021125098690390587]]\n"
     ]
    }
   ],
   "source": [
    "import erniebot\n",
    "\n",
    "erniebot.api_type = \"aistudio\"\n",
    "erniebot.access_token = \"<eb-access-token>\"\n",
    "\n",
    "# 将需要向量化的文本转化为list[str]输入\n",
    "response = erniebot.Embedding.create(\n",
    "    model=\"ernie-text-embedding\",\n",
    "    input=[\n",
    "        \"我是百度公司开发的人工智能语言模型，我的中文名是文心一言，英文名是ERNIE-Bot，可以协助您完成范围广泛的任务并提供有关各种主题的信息，比如回答问题，提供定义和解释及建议。如果您有任何问题，请随时向我提问。\",\n",
    "        \"2018年深圳市各区GDP\",\n",
    "    ],\n",
    ")\n",
    "\n",
    "# 使用response.get_result()获得模型返回结果，维度为(n,384)\n",
    "print(response.get_result())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3 文生图(Image Generation)\n",
    "\n",
    "文生图是指根据文本提示、图像尺寸等信息，使用文心大模型，自动创作图片。\n",
    "\n",
    "ERNIE Bot提供具备文生图能力的**ernie-vilg-v2**大模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"http://aigc-t2p.bj.bcebos.com/artist-long/118470787_0_final.png?authorization=bce-auth-v1%2F174bf5e9a7a84f55a8e85b1cc5d62b1d%2F2023-11-07T08%3A51%3A28Z%2F3600%2Fhost%2F8e4c096243272e66afd4713ef58bdf9e82a8e34635c37133c804ff13a7671b56\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import erniebot\n",
    "from IPython.display import Image\n",
    "\n",
    "# 注意需api_type与Chat Completion和Embedding不同\n",
    "erniebot.api_type = \"yinian\"\n",
    "erniebot.access_token = None\n",
    "erniebot.ak = \"<eb-ak>\"\n",
    "erniebot.sk = \"<eb-sk>\"\n",
    "\n",
    "response = erniebot.Image.create(model=\"ernie-vilg-v2\", prompt=\"雨后的桃花，8k，辛烷值渲染\", width=512, height=512)\n",
    "\n",
    "Image(url=response.get_result()[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "vscode": {
   "interpreter": {
    "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
